function showmsg(msg) {
	alert(msg);
}

function analyze(entity, d1,stat) {
	$.each(entity.dailySummaries[0].facilityBookings, function(index, value) {
		if ('valid' == value.status){
			$(stat[index]).find("div").removeClass("unselectable").removeClass("pending").removeClass("selected").addClass("selectable").attr("sid",value.bookingSession.id).attr("time",d1);
			$(stat[index]).find("i").removeClass("glyphicon-remove").addClass("glyphicon-ok");
		}else{
			$(stat[index]).find("div").removeClass("selectable").removeClass("pending").removeClass("selected").addClass("unselectable").attr("sid",value.bookingSession.id).attr("time",d1);
			$(stat[index]).find("i").removeClass("glyphicon-ok").addClass("glyphicon-remove");
			if ("pending" == value.status){
				$(stat[index]).find("div").addClass("pending");
			}
			
			
		}
	});
}
function getBookingStatus(d1, code) {
	var day = d1.getFullYear() + '-' + (d1.getMonth() + 1) + '-' + d1.getDate();
	getBookStatus(day, code);
}
function getBookStatus(day, code,stat) {
	var n = code.split(",");
	var submit = {
		type : "GET",
		dataType : "json",
		data     : {'code' : n},
		success : function(data) {
			if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
				return;
			} else {
				if (data.entity) {
					
					$.each(n, function(index, value) {
						analyze(data.entity[value], day,stat);
					})
				}
			}

		}
	};

	submit.url = '/cms-facility-booking/status/' + day + '/';
	$.ajax(submit);

}

function fybook() {
	if(!$(".agree")[0].checked){
		showmsg('please agree the Terms and Conditions.');
		return 
	}



	var sa = new Array();
	var ta = new Array();
	$('.timeslot.selectable.selected').each(function(index, value) {
			var time = $(this).attr('time');
			var sid = $(this).attr('sid');
			sa.push(sid);
			ta.push(time);
	});
	if (sa.length == 0) {
		showmsg('please choose an item');
		return;
	}
	if (sa.length > 1){
		showmsg('only one item allow');
		return;
		
	}
	var msg = 'Thank you. Your booking is acknowledged, please kindly approach the Management Office to submit the prescribed "Application Form" together refundable deposit of S$100.00 within 3 working days after date of booking for confirmed booking. Please read the following important terms & conditions:-'

		+'\n- Booking of Barbeque Pit can be made up to six (6) months in advance on a first-come-first served basis, and each Unit can book only 1 session at any time.'
		+'\n- Each Unit is allowed to book up to a maximum of twelve (12) sessions per calendar year. Cancellation of booking shall be made at least two (2) weeks before the booked date, failing which the Management reserves the right to forfeit the deposit. To discourage frivolous booking Resident(s) who booked and cancelled the booking for more than two (2) times within a calendar year will be barred from booking for next three (3) months.'
		+'\n- The deposit will be refunded only after the use of the barbeque pit and its surrounding areas are inspected.  Any expenses incurred by the Management for repairing damages caused in the surrounding areas shall be deducted from the deposit.'
		+'\n- The maximum number of Guests per Unit per session shall not exceed fifteen (15) persons.'

		+'\n\nFor more detailed terms & conditions, please browse By-laws under Useful Links. Thank you and have a nice day ahead.';
 
	var submit = {
		type : "POST",
		dataType : "json",
		data : {
			'time' : ta,
			'sid' : sa
		},
		success : function(data) {
			if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
				showmsg(data.message);
				
				return;
			} else {
				
//				getBookStatus(ta[0], sys)
				showmsg(msg);
				
				window.location="/member-booking-success"
			}

		}
	};
	submit.url = '/cms-facility-booking/booking/';
	$.ajax(submit);

}
$(document).ready(function() {

	$(document).delegate(".selectable","click",function(){
		 $(this).toggleClass("selected");
		});

	$('.bktn').click(function() {
		fybook();
	})

	$(".regalcalendar").RegalCalendar({ 
		onSelect : function(date,inst){
			var container =$(this).parents("div:first").siblings().find(".cal-value");
			container.parents("div:first").show();
			container.val(date);
		 	var x  =  date.split("/");
			var targetDate = x[2]+"-"+x[1]+"-"+x[0];
			var facility = ($(this).attr("facility"));
			getBookStatus(targetDate,facility,container.parents("div:first").find("dl dd"));
		},	
		show: 'mouseenter',
		tooltip: 'bootstrap',
		animation: 'fadeIn',
	});

});
